// XVideos.js for PSPTube

function XVideos_CheckURL( url, option )
{
	if (url.match("http://www.xvideos.com/") == "http://www.xvideos.com/"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a XVideos URL
*/
function XVideos_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null){return null; /* error getting page */}
	var video_url = contents.match(/&amp;flv_url\=(.*?)&/);
	var video_url = video_url[1];
	return video_url;
}

function XVideos_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 36;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents("http://www.xvideos.com/?k=" + keyword.replace(/ /g, "+"));
	} else{
	var num = (result.start - 1) / perpage;
	var contents = GetContents("http://www.xvideos.com/?k=" + keyword.replace(/ /g, "+") + "&p=" + num);
	}
	if(contents == null){return null; /* failed to get page */}


	result.total = 10000;


	while(1){
	pos = contents.indexOf("<td width=\"183\"><a href=\"");

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 25, contents.length);
		pos = contents.indexOf("\" class=\"miniature\"><img src=\"");
		var url = contents.substring(0, pos);
		//Get Thumbnail
		contents = contents.substring(pos + 30, contents.length);
		pos = contents.indexOf("\" onMouseOver=");
		var thumb_url = contents.substring(0, pos);
		//Title
		pos = contents.indexOf("<span class=\"red\" style=\"text-decoration:underline;\">");
		contents = contents.substring(pos + 53, contents.length);
		pos = contents.indexOf("</span>");
		var title = ImproveText(contents.substring(0, pos));
		//Length
		pos = contents.indexOf("<strong>");
		contents = contents.substring(pos + 8, contents.length);
		pos = contents.indexOf(")</strong> ");
		var length = contents.substring(0, pos);
		var mins = length.match(/\((.*?) min/);
		if(mins != null){
			var seconds = length.match(/min (.*?) sec/);
			var length = parseInt(mins[1]) * 60;
		} else{
			var seconds = length.match(/\((.*?) sec/);
			var length = 0;
		}
		if(seconds != null){length += parseInt(seconds[1]);}
		//Description
		contents = contents.substring(pos + 11, contents.length);
		pos = contents.indexOf(" </td>");
		var description = splitTo2Lines(ImproveText(contents.substring(0, pos)), 60);

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end   = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var XVideos = new Object();
XVideos.Name          = "XVideos";		/* Service name */
XVideos.Description   = "XVideos";		/* Service description */
XVideos.SearchDesc    = "XVideos";		/* Service search screen description */
XVideos.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
XVideos.CheckURL      = XVideos_CheckURL;	/* CheckURL function */
XVideos.GetURL        = XVideos_GetURL;		/* GetURL function */
XVideos.Search        = XVideos_Search;		/* Search function */

// add YouTube to site list
SiteList.push( XVideos );
